//
//  VCForMySelf.m
//  PCAForWar
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VCForMySelf.h"
#import "ShouCang.h"
#import "TuiSong.h"
#import "FanKui.h"
#import <SDImageCache.h>
#import <DKNightVersion.h>

#define WIDTH    [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHT   [UIScreen mainScreen].bounds.size.height / 667
@interface VCForMySelf () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *arrForS1;
@property (nonatomic, retain) NSMutableArray *arrForS2;
@property (nonatomic, retain) NSArray *arrForS3;
@property (nonatomic, retain) NSArray *arrForP1;
@property (nonatomic, retain) NSArray *arrForP2;
@property (nonatomic, retain) NSArray *arrForP3;
@property (nonatomic, retain) NSString *string;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UISwitch *swForNight;

@end

@implementation VCForMySelf


- (void)dealloc{
    
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [_arrForS1 release];
    [_arrForS2 release];
    [_arrForS3 release];
    [_arrForP1 release];
    [_arrForP2 release];
    [_arrForP3 release];
    [_tableView release];
    [_swForNight release];
    [_string release];
    [_label release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:22],}];
    
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(BackAction:)];
    
    self.navigationItem.leftBarButtonItems = @[itemLeft];
    
    [itemLeft release];
    
}

#pragma mark 夜间模式
- (void)viewWillAppear:(BOOL)animated {
    
    [self handleData];
    [self createTableView];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 224, self.view.frame.size.width - 5, 60)];
    
    [self.view addSubview:self.label];
    
    // 靠右
    self.label.textAlignment = 2;
    
    [_label release];
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
}

- (void)BackAction:(UIBarButtonItem *)barbuttonItem {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark 创建数组
- (void)handleData {
    
    NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    
    self.string = [NSString stringWithFormat:@"%.2fM",cacheSize];
    
    self.arrForS1 = @[@"我的收藏"];
    self.arrForS2 = @[@"推送设置",@"清除缓存"].mutableCopy;
    self.arrForS3 = @[@"意见反馈",@"夜间模式"];
    
    self.arrForP1 = @[@"ShouCang"];
    self.arrForP2 = @[@"Tuisong",@"Qingchu"];
    self.arrForP3 = @[@"Fankui",@"Yejianmoshi"];
    
}

- (void)createTableView {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    // 取消横线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.bounces = NO;
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            
            return self.arrForS1.count;
            
            break;
            
        case 1:
            
            return self.arrForS2.count;
            
            break;
            
        case 2:
            
            return self.arrForS3.count;
            
            break;
            
        default:
            
            return 0;
            
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForMySelf"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"poolForMySelf"];
    }
    
    if (indexPath.section == 0) {
        
        cell.textLabel.text = self.arrForS1[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:self.arrForP1 [indexPath.row]];
        
        cell.accessoryType = 1;
        
    } else if (indexPath.section == 1) {
        
        cell.textLabel.text = self.arrForS2[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:self.arrForP2 [indexPath.row]];
        
        if (indexPath.row != 1) {
            
            cell.accessoryType = 1;
            
        } else {
            
            self.label.text = self.string;
            
        }
        
    } else {
        
        cell.textLabel.text = self.arrForS3[indexPath.row];
        
        cell.imageView.image = [UIImage imageNamed:self.arrForP3 [indexPath.row]];
        
        if (indexPath.row == 0) {
            
            cell.accessoryType = 1;
            
        } else if (indexPath.row == 1) {
            
            self.swForNight = [[UISwitch alloc] initWithFrame:CGRectMake(315 * WIDTH, 15 * HEIGHT, 0, 0)];
            
            [cell.contentView addSubview:self.swForNight];
            
            [self.swForNight addTarget:self action:@selector(swForNightAction:) forControlEvents:UIControlEventValueChanged];
            
            [NSUserDefaults standardUserDefaults];
            
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
                
                self.swForNight.on = YES;
                
            } else {
                
                self.swForNight.on = NO;
                
            }
            
            [_swForNight release];
            
        }
        
    }
    
    // 夜间模式改变颜色
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    // 取消点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

// tableview头部更改夜间模式颜色
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    
    view.dk_backgroundColorPicker = DKColorWithColors([UIColor lightGrayColor], [UIColor grayColor]);
    
    return view;
}

#pragma mark 创建清除缓存
- (void)alertControllerWithIndexPath:(NSIndexPath *)indexPath {
    
    // 缓存大小
    NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    
    NSString *string = [NSString stringWithFormat:@"清除:%.2fM",cacheSize];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *OK = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //清除缓存
        [[SDImageCache sharedImageCache] clearDisk];
        
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        
        float cacheSize = size / 1024.0 / 1024.0;
        
        self.string = [NSString stringWithFormat:@"%.2fM",cacheSize];
        
        [self.tableView reloadData];
        
    }];
    
    [alert addAction:cancel];
    [alert addAction:OK];
    [self presentViewController:alert animated:YES completion:nil];
    
};

#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        self.hidesBottomBarWhenPushed = YES;
        
        ShouCang *sc = [[ShouCang alloc] init];
        
        [self.navigationController pushViewController:sc animated:YES];
        
        [sc release];
        
        
    } else if (indexPath.section == 1) {
        
        self.hidesBottomBarWhenPushed = YES;
        
        if (indexPath.row == 0) {
            
            self.hidesBottomBarWhenPushed = YES;
            
            TuiSong *ts = [[TuiSong alloc] init];
            
            [self.navigationController pushViewController:ts animated:YES];
            
            [ts release];
            
        } else {
            
            [self alertControllerWithIndexPath:indexPath];
            
        }
        
    } else {
        
        if (indexPath.row == 0) {
            
            self.hidesBottomBarWhenPushed = YES;
            
            FanKui *fk = [[FanKui alloc] init];
            
            [self.navigationController pushViewController:fk animated:YES];
            
            [fk release];
            
        }
        
    }
    
}

- (void)swForNightAction:(UISwitch *)sw {
    
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
        
        [DKNightVersionManager dawnComing];
        
        [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"夜间模式"];
        
    } else {
        
        [DKNightVersionManager nightFalling];
        
        [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"夜间模式"];
        
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
